/**
 * <p>Copyright (c) 2011 深圳市鹏途信息技术有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 *
 * <p>项目名称 ： 	东莞交通工程质量监督综合业务系统        </p>
 * <p>创建者   :	libitum
 *
 * <p>描   述  :   ApplicationManager.java for com.pengtu.service.business    </p>
 *
 * <p>最后修改 : $: 2011-7-12-下午04:41:42 v 1.0.0	 libitum   $     </p>
 *
 */

package com.pengtu.service.business;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.jbpm.api.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.pengtu.Constants;
import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.dao.business.ApplMaterialDao;
import com.pengtu.dao.business.ApplNoticeDao;
import com.pengtu.dao.business.ApplPetitionDao;
import com.pengtu.dao.business.FlowApplicationDao;
import com.pengtu.dao.business.SuperviserDao;
import com.pengtu.dao.quality.FlowProjectDao;
import com.pengtu.entity.app.User;
import com.pengtu.entity.business.Superviser;
import com.pengtu.entity.business.application.ApplMaterial;
import com.pengtu.entity.business.application.ApplNotice;
import com.pengtu.entity.business.application.ApplPetition;
import com.pengtu.entity.business.application.FlowApplication;
import com.pengtu.entity.history.HistoryProject;
import com.pengtu.entity.project.ProjectInfo;
import com.pengtu.entity.quality.FlowProject;
import com.pengtu.service.BaseManager;
import com.pengtu.service.history.HistoryProjectManager;
import com.pengtu.service.project.ProjectManager;

/**
 * 
 * ApplicationManager
 * 
 * 2011-7-12 下午04:41:42
 * 
 * @version 1.0.0
 * 
 */
@Component
@Transactional
public class ApplicationManager extends BaseManager {
	private HistoryProjectManager historyProjectManager;
	private ProjectManager projectManager;

	private FlowApplicationDao flowApplicationDao;
	private ApplPetitionDao applPetitionDao;
	private ApplNoticeDao applNoticeDao;
	private ApplMaterialDao applMaterialDao;
	private SuperviserDao superviserDao;
	private FlowProjectDao flowProjectDao;

	@Autowired
	public void setHistoryProjectManager(
			HistoryProjectManager historyProjectManager) {
		this.historyProjectManager = historyProjectManager;
	}

	@Autowired
	public void setProjectManager(ProjectManager projectManager) {
		this.projectManager = projectManager;
	}

	@Autowired
	public void setFlowApplicationDao(FlowApplicationDao flowApplicationDao) {
		this.flowApplicationDao = flowApplicationDao;
	}

	@Autowired
	public void setApplPetitionDao(ApplPetitionDao applPetitionDao) {
		this.applPetitionDao = applPetitionDao;
	}

	@Autowired
	public void setApplNoticeDao(ApplNoticeDao applNoticeDao) {
		this.applNoticeDao = applNoticeDao;
	}

	@Autowired
	public void setApplMaterialDao(ApplMaterialDao applMaterialDao) {
		this.applMaterialDao = applMaterialDao;
	}

	@Autowired
	public void setSuperviserDao(SuperviserDao superviserDao) {
		this.superviserDao = superviserDao;
	}

	@Autowired
	public void setFlowProjectDao(FlowProjectDao flowProjectDao) {
		this.flowProjectDao = flowProjectDao;
	}

	/**
	 * 启动流程 start: 适用:
	 * 
	 * @param flowApplication
	 * @param deploymentId
	 * @param user
	 * @param historyProject
	 * @return
	 * @exception
	 * @since 1.0.0
	 */
	public void start(FlowApplication flowApplication, String deploymentId,
			User user, ProjectInfo projectInfo) {
		Map<String, Object> variables = new HashMap<String, Object>();
		variables.put(Constants.APPLY_ID, flowApplication.getId());
		super.start(flowApplication.getId(), deploymentId,
				"apply", variables);
		projectManager.saveOrupdateProj(projectInfo);
		flowApplicationDao.save(flowApplication);
	}

	/**
	 * 保存 save: 适用:
	 * 
	 * @param flowApplication
	 * @param historyProject
	 * @exception
	 * @since 1.0.0
	 */
	public void save(FlowApplication flowApplication,
			HistoryProject historyProject) {
		flowApplicationDao.save(flowApplication);
		historyProject.setKey(flowApplication.getId());
		historyProjectManager.save(historyProject);
	}

	public void save(FlowApplication flowApplication) {
		flowApplicationDao.save(flowApplication);
	}

	public void save(ApplPetition applPetition) {
		applPetitionDao.save(applPetition);
	}

	/**
	 * 保存流程表和业务受理 save: 适用:
	 * 
	 * @param applPetition
	 * @param flowApplication
	 * @exception
	 * @since 1.0.0
	 */
	public void save(ApplPetition applPetition, FlowApplication flowApplication) {
		flowApplicationDao.save(flowApplication);
		applPetition.setFlowApplication(flowApplication);
		applPetitionDao.save(applPetition);
	}

	public void save(ApplMaterial applMaterial) {
		applMaterialDao.save(applMaterial);
	}

	public void save(List<ApplMaterial> materials) {
		for (ApplMaterial entity : materials) {
			applMaterialDao.save(entity);
		}
	}

	public void save(ApplNotice applNotice) {
		applNoticeDao.save(applNotice);
	}

	public void save(Superviser superviser) {
		superviserDao.save(superviser);
	}

	/**
	 * 接受任务 takeTask: 适用:
	 * 
	 * @param taskId
	 * @param key
	 * @param user
	 * @param historyProject
	 * @exception
	 * @since 1.0.0
	 */
	public void takeTask(String taskId, String key, User user,
			HistoryProject historyProject) {
		super.takeTask(taskId, user);
		historyProject.setKey(key);
		historyProjectManager.save(historyProject);
	}

	/**
	 * 查询待办任务 find: 适用:
	 * 
	 * @param user
	 * @param projectInfo
	 * @return
	 * @exception
	 * @since 1.0.0
	 */
	public List<FlowApplication> find(User user, ProjectInfo projectInfo) {
		List<FlowApplication> list = new ArrayList<FlowApplication>();
		List<Task> tasks1 = super.findGroupTasks(user);
		List<Task> tasks2 = super.findPersonalTasks(user);
		if (tasks1.size() != 0 || tasks1 != null) {
			for (Task task : tasks1) {
				String taskId = task.getId();
				String exectionId = task.getExecutionId();
				String arg = exectionId.substring(0, 5);
				if ("apply".equals(arg)) {
					String id = (String) super.getVariableByTaskId(taskId,
							Constants.APPLY_ID);
					FlowApplication flowApplication = flowApplicationDao
							.get(id);
					if (flowApplication != null
							& flowApplication.getProjectInfo().getId()
									.equals(projectInfo.getId())) {
						flowApplication.setTaskId(taskId);
						list.add(flowApplication);
					}
				}
			}
		}
		if (tasks2.size() != 0 || tasks2 != null) {
			for (Task task : tasks2) {
				String taskId = task.getId();
				String exectionId = task.getExecutionId();
				String arg = exectionId.substring(0, 5);
				if ("apply".equals(arg)) {
					String id = (String) super.getVariableByTaskId(taskId,
							Constants.APPLY_ID);
					FlowApplication flowApplication = flowApplicationDao
							.get(id);
					if (flowApplication != null
							& flowApplication.getProjectInfo().getId()
									.equals(projectInfo.getId())) {
						flowApplication.setTaskId(taskId);
						list.add(flowApplication);
					}
				}
			}
		}
		return list;
	}

	public List<FlowApplication> getFlowApplicationsByPro(
			String propertyName, String value) {
		return flowApplicationDao.findBy(propertyName, value);
	}
	
	/**
	 * 待接任务 findGroupTasks: 适用:
	 * 
	 * @param user
	 * @param projectInfo
	 * @return
	 * @exception
	 * @since 1.0.0
	 */
	public FlowApplication findGroupTasks(User user, ProjectInfo projectInfo) {
		FlowApplication flowApplication = null;
		List<Task> taskList = super.findGroupTasks(user);
		for (Task task : taskList) {
			String taskId = task.getId();
			String exectionId = task.getExecutionId();
			String arg = exectionId.substring(0, 5);
			if ("apply".equals(arg)) {
				String id = (String) super.getVariableByTaskId(taskId,
						Constants.APPLY_ID);
				flowApplication = flowApplicationDao.get(id);
				if (flowApplication != null
						& flowApplication.getProjectInfo().getId()
								.equals(projectInfo.getId())) {
					flowApplication.setTaskId(taskId);
				}
			}
		}
		return flowApplication;
	}

	/**
	 * 待办任务 findPersonalTasks: 适用:
	 * 
	 * @param user
	 * @param projectInfo
	 * @return
	 * @exception
	 * @since 1.0.0
	 */
	public FlowApplication findPersonalTasks(User user, ProjectInfo projectInfo) {
		List<Task> taskList = super.findPersonalTasks(user);
		for (Task task : taskList) {
			String taskId = task.getId();
			String exectionId = task.getExecutionId();
			String arg = exectionId.substring(0, 5);
			if ("apply".equals(arg)) {
				String id = (String) super.getVariableByTaskId(taskId,
						Constants.APPLY_ID);
				FlowApplication flowApplication = flowApplicationDao.get(id);
				if (flowApplication != null) {
					if (StringUtils.equals(flowApplication.getProjectInfo()
							.getId(), projectInfo.getId())) {
						flowApplication.setTaskId(taskId);
						return flowApplication;
					}
				}
			}
		}
		return null;
	}

	/**
	 * 判断是否是待办任务 isFindPersonalTasks: 适用:
	 * 
	 * @param user
	 * @param id
	 * @return
	 * @exception
	 * @since 1.0.0
	 */
	public String isFindPersonalTasks(User user, String id) {
		return super.isPersonalTasks(user, id, Constants.APPLY_ID);
	}

	/**
	 * 提交任务 completeTask: 适用:
	 * 
	 * @param flowApplication
	 * @param outcome
	 * @param variables
	 * @param historyProject
	 * @param projectInfo
	 * @exception
	 * @since 1.0.0
	 */
	public void completeTask(FlowApplication flowApplication, String outcome,
			Map<String, Object> variables, HistoryProject historyProject) {
		super.completeTask(flowApplication.getTaskId(), outcome, variables);
		historyProjectManager.save(historyProject);
		flowApplicationDao.save(flowApplication);
	}

	public FlowApplication getFlowApplication(String id) {
		return flowApplicationDao.get(id);
	}

	public Page<FlowApplication> getFlowApplications(
			final Page<FlowApplication> page, final QueryFilter queryfilter) {
		return flowApplicationDao.findPage(page, queryfilter);
	}

	public FlowApplication getFlowApplication(ProjectInfo projectInfo) {
		return flowApplicationDao.findUniqueBy("projectInfo", projectInfo);
	}

	public ApplPetition getApplPetition(String id) {
		return applPetitionDao.get(id);
	}

	public ApplPetition getApplPetition(FlowApplication flowApplication) {
		return applPetitionDao.findUniqueBy("flowApplication", flowApplication);
	}

	public ApplPetition getApplPetition(ProjectInfo projectInfo) {
		return applPetitionDao.findUniqueBy("projId", projectInfo.getId());
	}

	public ApplMaterial getApplMaterial(String id) {
		return applMaterialDao.get(id);
	}

	public List<ApplMaterial> getApplMaterials(FlowApplication flowApplication) {
		return applMaterialDao.findBy("flowApplication", flowApplication);
	}

	public ApplNotice getApplNotice(String id) {
		return applNoticeDao.get(id);
	}

	public ApplNotice getApplNotice(FlowApplication flowApplication) {
		return applNoticeDao.findUniqueBy("flowApplication", flowApplication);
	}

	public Superviser getSuperviser(String id) {
		return superviserDao.get(id);
	}

	public Page<Superviser> search(Page<Superviser> page, QueryFilter fileter) {
		return superviserDao.findPage(page, fileter);
	}

	/**
	 * 根据key查询
	 * 
	 * @param key
	 * @return
	 */
	public FlowProject getByKey(String key) {
		return flowProjectDao.findUniqueBy("key", key);
	}

	public List<FlowApplication> findByPropty(String propertyName, String value) {
		// TODO Auto-generated method stub
		return flowApplicationDao.findBy(propertyName, value);
	}

}
